package cn.itsource.controller;

import cn.itsource.domain.Teacher;
import cn.itsource.service.ITeacherService;
import cn.itsource.utils.ExcelUtil;
import cn.itsource.utils.JSONResult;
import org.apache.coyote.Response;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制层
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    // 依赖注入
    @Autowired
    private ITeacherService teacherService;

    /**
     * 保存或修改数据
     * @param teacher
     * @return
     */
    @PostMapping
    public JSONResult save(@RequestBody Teacher teacher) {
        try {
            teacherService.save(teacher);
            return JSONResult.ok("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("保存失败:" + e.getMessage());
        }
    }

    /**
     * 根据id删除教师数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        try {
            teacherService.delete(id);
            return JSONResult.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("删除失败" + e.getMessage());
        }
    }

    /**
     * 根据id查询教师数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public JSONResult get(@PathVariable("id") Long id) {
        try {
            return JSONResult.ok("查询成功",teacherService.get(id));
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("查询失败");
        }
    }

    /**
     * 查询全部教师数据
     * @return
     */
    @PatchMapping
    public JSONResult getAll() {
        try {
            return JSONResult.ok("查询全部数据成功",teacherService.getAll());
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("查询全部数据失败");
        }
    }

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Teacher> all = teacherService.getAll();
        ExcelUtil.exportExcel(all,"智慧校园人员信息","教师信息表",Teacher.class,"Teacheres",response);
    }

    @PostMapping("/import")
    public JSONResult importExcel(MultipartFile file) {
        try {
            List<Teacher> teachers = ExcelUtil.importExcel(file, 1, 1, Teacher.class);
            teacherService.batchSave(teachers);
            return JSONResult.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return JSONResult.fail(e.getMessage());
        }
    }

    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil.exportExcel(new ArrayList<Teacher>(),"教师信息表模板","副本",Teacher.class,"教师信息表模板",response);
    }
}
